package org.smartboot.flow.spring.extension;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author qinluo
 * @date 2023/1/31 22:15
 * @since 1.0.0
 */
public class SmartFlowRegistrar implements ImportBeanDefinitionRegistrar {

    private static final String NOTIFIER_PROCESS = "smart-flow-notifer-processor";
    private static final String REGISTRY_NAME = "smart-flow-registry-processor";

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
        registerAll(registry);
    }

    public static void registerAll(BeanDefinitionRegistry registry) {
        registerNotifier(registry);
        registerRegistry(registry);
    }

    private static void registerNotifier(BeanDefinitionRegistry registry) {
        if (registry.containsBeanDefinition(NOTIFIER_PROCESS)) {
            return;
        }
        RootBeanDefinition definition = new RootBeanDefinition();
        definition.setBeanClass(NotifierProcessor.class);
        registry.registerBeanDefinition(NOTIFIER_PROCESS, definition);
    }

    private static void registerRegistry(BeanDefinitionRegistry registry) {
        if (registry.containsBeanDefinition(REGISTRY_NAME)) {
            return;
        }
        RootBeanDefinition definition = new RootBeanDefinition();
        definition.setBeanClass(SmartFlowBeanFactoryRegistry.class);
        registry.registerBeanDefinition(REGISTRY_NAME, definition);
    }
}
